package com.codefarm.phone.mtsh.doctor.adapter;

import java.util.LinkedList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.codefarm.phone.mtsh.doctor.R;
import com.codefarm.phone.mtsh.doctor.activity.CommonBaseActivity;
import com.fortysevendeg.swipelistview.SwipeListView;

public class MainListAdapter<T> extends BaseAdapter {

	public MainListAdapter(CommonBaseActivity activity,SwipeListView swipeListView){
		setActivity(activity);
		setSwipeListView(swipeListView);
		inflater = getActivity().getLayoutInflater();
	}
	
	private CommonBaseActivity activity;
	private SwipeListView swipeListView;

	private LayoutInflater inflater;
	
	public CommonBaseActivity getActivity() {
		return activity;
	}

	public void setActivity(CommonBaseActivity activity) {
		this.activity = activity;
	}
	
	public SwipeListView getSwipeListView() {
		return swipeListView;
	}

	public void setSwipeListView(SwipeListView swipeListView) {
		this.swipeListView = swipeListView;
	}

	private List<T> array = new LinkedList<T>();
	
	@Override
	public int getCount() {
		return array.size();
	}

	@Override
	public T getItem(int position) {
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder _h = null;
		convertView = inflater.inflate(R.layout.item_main, null);
		_h = new Holder();
		_h.txt_title = (TextView) convertView.findViewById(R.id.txt_title);
		_h.btn_del = (Button) convertView.findViewById(R.id.btn_del);
		T _t = getItem(position);
		if(_t instanceof String){
			if(_h.txt_title!=null){
				_h.txt_title.setText((String)_t);
			}
		}
		if(_h.btn_del!=null){
			_h.btn_del.setClickable(true);
			_h.btn_del.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					getSwipeListView().dismiss(position);
				}
			});
		}
		return convertView;
	}
	
	class Holder{
		public TextView txt_title;
		public Button btn_del;
	}

	public void add(T t){
		array.add(t);
	}
	
	public void remove(int position){
		array.remove(position);
	}
	
}
